package org.testis.data.parser;

import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import org.testis.testdriver.AtofEnumaration;
import org.testis.testdriver.AtofEnumaration.AtofWebAutomationModuleAction;
import org.testis.webautomationmodule.data.WebAutomationModuleDataObject;
import org.orm.PersistentException;
import org.testis.orm.refinedclasses.DynamicMapDesignTreeNode;
import org.testis.orm.refinedclasses.DynamicMapObject;
import org.testis.orm.refinedclasses.DynamicMapParameter;


/**
 *
 * @author nurhat
 */
public class WAMHandleDutIpDopDecorator extends WAMDopDecorator
{

    public WAMHandleDutIpDopDecorator(
        WebAutomationModuleDataObject data,
        DynamicMapDesignTreeNode root)
    {
        super(data,
              root);
    }


    @Override
    protected boolean setBehavior()
    {

        DynamicMapObject obj = getRoot().getObject();
        DynamicMapParameter parFactorySettingsIp = obj.getParameterByName(
            AtofModuleParameterNames.WEB_AUTO_FACTORY_SETTINGS_IP);
        String factorySettingsIp = getParameterValue(parFactorySettingsIp);

        DynamicMapParameter parIpAddressTxtAbstractId = obj.getParameterByName(
            AtofModuleParameterNames.WEB_AUTO_IP_ADDRESS_TXT_ABSTRACT_ID);
        String ipAddressTxtAbstractId = getParameterValue(
            parIpAddressTxtAbstractId);
        DynamicMapParameter parIpAddressBtnAbstractId = obj.getParameterByName(
            AtofModuleParameterNames.WEB_AUTO_IP_ADDRESS_BTN_ABSTRACT_ID);
        String ipAddressBtnAbstractId = getParameterValue(
            parIpAddressBtnAbstractId);
        String testBedIp = "";
        if (getData().getModuleDataToApply().containsKey(
            AtofModuleParameterNames.WEB_AUTO_DEVICE_TESTBED_IP)) {
            testBedIp =
                getData().getModuleDataToApply().get(
                AtofModuleParameterNames.WEB_AUTO_DEVICE_TESTBED_IP);
        }

        getData().setActionType(AtofEnumaration.AtofWebAutomationModuleAction.Set.
                                name());
        if (getData().getDataToApply().containsKey(
            AtofModuleParameterNames.WEB_AUTO_RESET_TO_FACTORY_SETTINGS)) {
            getData().setActionType(AtofWebAutomationModuleAction.Set.name());

            getData().getDataToApply().clear();

            getData().getDataToApply().put(ipAddressTxtAbstractId,
                                           factorySettingsIp);
            getData().getDataToApply().put(ipAddressBtnAbstractId,
                                           "");

        }
        else if (getData().getDataToApply().containsKey(
            AtofModuleParameterNames.WEB_AUTO_RESET_TO_TEST_BED_IP)) {
            getData().setActionType(AtofWebAutomationModuleAction.Set.name());
            getData().getDataToApply().clear();

            getData().getDataToApply().put(ipAddressTxtAbstractId,
                                           testBedIp);
            getData().getDataToApply().put(ipAddressBtnAbstractId,
                                           "");

        }
        else if (getData().getDataToApply().containsKey(
            AtofModuleParameterNames.WEB_AUTO_VERIFY_FACTORY_SETTINGS_IP)) {
            getData().getDataToApply().clear();
            getData().setActionType(
                AtofWebAutomationModuleAction.GetAndVerify.name());

            getData().getDataToApply().clear();
            getData().getDataToApply().put(ipAddressTxtAbstractId,
                                           factorySettingsIp);

        }
        else if (getData().getDataToApply().containsKey(
            AtofModuleParameterNames.WEB_AUTO_VERIFY_TEST_BED_IP)) {
            getData().getDataToApply().clear();
            getData().setActionType(
                AtofWebAutomationModuleAction.GetAndVerify.name());

            getData().getDataToApply().put(ipAddressTxtAbstractId,
                                           testBedIp);
        }
        else {
            LogSenderService.putMessageOnQueue(
                "WebAutomationDataObjectParser",
                AtofLogLevel.ERROR,
                "No parameter given for HandleDutIpAction",
                getData().getMessageId());
            return false;
        }

        return true;
    }
}
